'''
Created on Dec 1, 2012

@author: quirino
'''
from .imports import pycurl

class Authentication(object):
    '''
    Defines the archetype method for authentication provider classes
    '''


    def __init__(self, username, password):
        '''
        Creates a new authentication instance
        '''
        self.username = username
        self.password = password
    
    def type(self): 
        """
        Declare the authentication method type using PyCurl options
        """
        raise NotImplementedError()
    
    def credentials(self):
        """
        Return the credential (username and password) on the right format
        for this authentication type, credentials will be set on cURL using 
        pycurl.USERPWD option.
        """
        raise NotImplementedError()
    
    def configure(self, conn):
        """
        Configure the connection wrapper accordingly to the authorization 
        needs
        @param conn: the connection wrapper
        """
        raise NotImplementedError()
    
class BasicAuthentication(Authentication):
    """
    HTTP Basic authentication. This is the default choice, and the only method 
    that is in wide-spread use and supported virtually everywhere. This sends 
    the user name and password over the network in plain text, easily captured 
    by others.
    """
    def __init__(self, username, password):
        super(BasicAuthentication, self).__init__(username, password)
        
    def type(self, **kwargs):
        """
        Declare the authentication method type using pycurl.HTTPAUTH_BASIC 
        option
        """
        return pycurl.HTTPAUTH_BASIC
    def credentials(self):
        """
        Build and return the credential string as username:password
        """
        return "%s:%s" % (self.username, self.password)
    
    def configure(self, conn):
        """
        Configure the connection wrapper accordingly to the authorization 
        needs
        @param conn: the connection wrapper
        """
        conn.option(pycurl.HTTPAUTH, self.type())
        conn.option(pycurl.USERPWD, self.credentials())

class DigestAuthentication(Authentication):
    """
    HTTP Digest authentication. Digest authentication is defined in RFC 2617 
    and is a more secure way to do authentication over public networks than 
    the regular old-fashioned Basic method.
    """
    def __init__(self, username, password):
        super(DigestAuthentication, self).__init__(username, password)
        
    def type(self, **kwargs):
        """
        Declare the authentication method type using pycurl.HTTPAUTH_BASIC 
        option
        """
        return pycurl.HTTPAUTH_DIGEST
    def credentials(self):
        """
        Build and return the credential string as username:password
        """
        return "%s:%s" % (self.username, self.password)
    
    def configure(self, conn):
        """
        Configure the connection wrapper accordingly to the authorization 
        needs
        @param conn: the connection wrapper
        """
        conn.option(pycurl.HTTPAUTH, self.type())
        conn.option(pycurl.USERPWD, self.credentials())

class DigestIEAuthentication(Authentication):
    """
    HTTP Digest authentication with an IE flavor. Digest authentication is 
    defined in RFC 2617 and is a more secure way to do authentication over 
    public networks than the regular old-fashioned Basic method. The IE 
    flavor is simply that libcurl will use a special "quirk" that IE is known 
    to have used before version 7 and that some servers require the client to 
    use. (This define was added in 7.19.3) 
    """
    def __init__(self, username, password):
        super(DigestIEAuthentication, self).__init__(username, password)
        
    def type(self, **kwargs):
        """
        Declare the authentication method type using pycurl.HTTPAUTH_BASIC 
        option
        """
        return pycurl.HTTPAUTH_DIGEST
    def credentials(self):
        """
        Build and return the credential string as username:password
        """
        return "%s:%s" % (self.username, self.password)
    
    def configure(self, conn):
        """
        Configure the connection wrapper accordingly to the authorization 
        needs
        @param conn: the connection wrapper
        """
        conn.option(pycurl.HTTPAUTH, self.type())
        conn.option(pycurl.USERPWD, self.credentials())